Ext.define('extjs6test.view.core.DictManagerController', {
    extend: 'extjs6test.view.base.BaseManagerController',
    mixins: {
        "baseewc":"extjs6test.view.base.BaseEditWindowController"
    },
    requires: [
        "extjs6test.view.base.BaseManagerController",
        "extjs6test.view.base.BaseEditWindowController",
        "extjs6test.view.core.DictEditWindow"
    ],
    alias: 'controller.dictmngr',
    onBaseQuery: function (button, evt) {
        // console.log("this is onBaseQuery");
        var grid = this.lookupReference("dictGrid");
        var queryForm = this.lookupReference("queryForm");

        if (queryForm.isValid()) {
            var queryData = queryForm.getValues();
            var store = grid.getStore();

            for (var k in queryData) {
                store.getProxy().setExtraParam(k, queryData[k]);
            }
            store.load(function(records, operation, success) {
                store.getProxy().extraParams = {};
            });

        }
    },
    onBaseAdd: function (button, evt) {
        // console.log("this is onBaseAdd");
        var store = this.lookupReference("dictGrid").getStore();
        var record = Ext.create(store.model);
        record.data.id = null;
        var addWin = new extjs6test.view.core.DictEditWindow({formStore: store, formRecord: record,mode:"new"});
        addWin.show();
    },
    onAddItem: function (button, evt) {
        var grid = this.lookupReference("itemGrid");
        var itemStore = grid.getStore();
        var record = Ext.create(itemStore.model);
        // record.data.id = null;
        itemStore.add(record);
    },
    onRemoveItem: function (button, evt) {
        var grid = this.lookupReference("itemGrid");
        var itemStore = grid.getStore();
        var selection = grid.getSelection();
        if (selection.length) {
            itemStore.remove(selection);
        }
    },
    onOkButtonClick: function (button, evt) {
        var ctl = this;

        var win = ctl.getView();
        var formStore = win.formStore;
        var form = win.down('form');
        var itemStore = ctl.lookupReference("itemGrid").getStore();

        if (form.isValid()) {
            form.updateRecord();

            var record = form.getRecord();
            if(itemStore.getNewRecords().length)
                record.set("addDictItems",Ext.Array.map(itemStore.getNewRecords(),function (rec) {
                    var data = {};
                    Ext.each(rec.fields,function (field) {
                        var value = rec.get(field.getName());
                        if(field.serialize){
                            value = field.serialize(value,rec);
                        }
                        data[field.getName()] = value;
                    });
                    data.id = null;
                    data.orderNum = 0;
                    return data;

                }));
            if(itemStore.getUpdatedRecords().length)
                record.set("updateDictItems",Ext.Array.map(itemStore.getUpdatedRecords(),function (rec) {
                    var data = {};
                    Ext.each(rec.fields,function (field) {
                        var value = rec.get(field.getName());
                        if(field.serialize){
                            value = field.serialize(value,rec);
                        }
                        data[field.getName()] = value;
                    });
                    return data;
                }));
            if(itemStore.getRemovedRecords().length)
                record.set("removeDictItems",Ext.Array.map(itemStore.getRemovedRecords(),function (rec) {
                    var data = {};
                    Ext.each(rec.fields,function (field) {
                        var value = rec.get(field.getName());
                        if(field.serialize){
                            value = field.serialize(value,rec);
                        }
                        data[field.getName()] = value;
                    });
                    return data;
                }));

            var isContinue = form.down('checkbox[name="isContinue"][value=true]');

            if(win.mode == "new"){
                formStore.add(record);
            }
            formStore.sync({
                success:function (batch, opts) {
                    formStore.commitChanges();
                    formStore.load();
                    if (!isContinue) {
                        win.close();
                    }
                    else {
                        var record = Ext.create(formStore.model);
                        record.data.id = null;
                        form.loadRecord(record);
                        form.reset();
                        isContinue.setValue(true);
                    }
                },
                failure:function (batch, opts) {
                    console.log(batch);
                    console.log(opts);
                    Ext.Msg.alert("错误","参数格式错误");
                }
            });


        }
    },
    onBaseModify:function (button,evt) {
        var c = this;
        var grid=c.lookupReference("dictGrid");
        var store = grid.getStore();
        if (grid.getSelection().length > 0) {
            var record = grid.getSelection()[0];
            var editWindow = new extjs6test.view.core.DictEditWindow({mode:"modify",formStore:store,
                formRecord:record});
            editWindow.show();
        }


    },
    onModifyModeAfterRender:function (win) {
        var c = this;
        c.mixins.baseewc.onModifyModeAfterRender.call(this,win);
        var itemStore = c.lookupReference("itemGrid").getStore();
        itemStore.getProxy().setExtraParam("dictId",win.formRecord.get("id"));
        itemStore.load();
    }
});